package com.bazinga.contact.view;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.bazinga.contact.model.SingleAppliedJob;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AppliedJobsAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> session;
	private final String dbServer = "127.0.0.1";
	private List<SingleAppliedJob> jobList;
	public List<SingleAppliedJob> getJobList() {
		return jobList;
	}
	public void setJobList(List<SingleAppliedJob> jobList) {
		this.jobList = jobList;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	/**
     * @see HttpServlet#HttpServlet()
     */
	
	public String execute() {
		this.session = ActionContext.getContext().getSession();
		if(!session.containsKey("logged-in")) {
			return "login";
		}
		
		String userid = session.get("userid").toString();
		String dbUrl = "jdbc:mysql://" + dbServer + ":3306/";
		String dbName = "bazinga";
		String driverName = "org.gjt.mm.mysql.Driver";
		String username = "root";
		String password = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		String searchQuery = "select J.jobid, J.organisation, J.title, A.status from jobdetails J,applications A where J.jobid = A.jobid and A.userid=" + userid + " ORDER by J.jobid;";
		try
		{
			Class.forName(driverName).newInstance();
			conn = DriverManager.getConnection(dbUrl + dbName, username, password);
			stmt = conn.createStatement();
		}
		catch (Exception e) {
			System.out.println(e.toString());
		}
		try {
			rs = stmt.executeQuery(searchQuery);
		} 
		
		catch (SQLException e) {
			e.printStackTrace();
			return "error";
		}
		
		jobList = new ArrayList<SingleAppliedJob>();
		try {
			while(rs.next()) {
				String jobid = rs.getString("jobid").toString();
				int status = Integer.parseInt(rs.getString("status").toString());
				String organisation = rs.getString("organisation").toString();
				String title = rs.getString("title").toString();
				
				SingleAppliedJob temp = new SingleAppliedJob();
				temp.setJobid(jobid);
				if(status == 0)
					temp.setStatus("Pending");
				else if(status == 1)
					temp.setStatus("In Progress");
				else
					temp.setStatus("Completed");
				temp.setOrganization(organisation);
				temp.setTitle(title);
				
				jobList.add(temp);
			}
		}
		
		catch(SQLException ex) {
			ex.printStackTrace();
		}
		
		catch(Exception ex) {
			ex.printStackTrace();
		}
		return "success";
	}
    public AppliedJobsAction() {
        super();
        // TODO Auto-generated constructor stub
    }

}
